package com.tm.fiverole.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tm.fiverole.entity.RoleEntity;
import com.tm.fiverole.entity.UserEntity;
import com.tm.fiverole.entity.UserRoleEntity;
import com.tm.fiverole.entity.Vo.UserVo;
import com.tm.fiverole.mapper.UserMapper;
import com.tm.fiverole.mapper.UserRoleMapper;
import com.tm.fiverole.result.Result;
import com.tm.fiverole.result.ResultEnum;
import com.tm.fiverole.service.UserService;
import com.tm.fiverole.ulits.Pageutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author wangSang
 * @create 2022-08-18 14:47
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private ServiceImpl service;

    //登录验证
    @Override
    @Transactional
    public Result LogInUser(UserEntity user) {
        //查询
        UserEntity userEntity = userMapper.queryByUser(user.getUsername());
        //判断是否为空
        if (userEntity != null) {
            //判断密码是否一致
            if (userEntity.getPassword().equals(user.getPassword())) {
                //添加最后一次等陆
                userEntity.setLastLoginTime(new Date());
                //进行修改
                userMapper.updateUserTime(userEntity);

                return Result.SUCCESS(ResultEnum.LOGIN_SUCCESS);
            } else {
                //密码错误基础上加一
                userEntity.setPwdErrorCount(userEntity.getPwdErrorCount() + 1);
                //进行修改
                userMapper.updateUser(userEntity);

            }
        }


        return Result.ERROR(ResultEnum.LOGIN_ERROR);
    }

    /**
     * 分页查询用户信息
     *
     * @param user
     * @return
     */
    @Override
    public Result queryUser(UserVo user) {
        Pageutil pageutil = new Pageutil();
        Long count = userMapper.queryCount(user);
        pageutil.setTotalCount(count);
        List<UserEntity> list = userMapper.queryUser(user);
        pageutil.setList(list);


        return Result.SUCCESS(pageutil);
    }

    /***
     * 新增用户表
     * @param user
     * @return
     */
    @Override
    @Transactional
    public Result addUser(UserEntity user) {

        UserEntity userEntity = userMapper.queryByName(user.getUsername());
        if (userEntity != null) {

            return Result.ERROR(ResultEnum.ADD_ERROR);
        }
        user.setCreateTime(new Date());
        userMapper.addUser(user);
        for (int i = 0; i < user.getRoleEntitylist().size(); i++) {
            UserRoleEntity userRoleEntity = new UserRoleEntity();
            RoleEntity roleEntity = user.getRoleEntitylist().get(i);
            userRoleEntity.setUserId(user.getId());
            userRoleEntity.setRoleId(roleEntity.getId());
            userRoleEntity.setCreateUser("毛毛");
            userRoleEntity.setCreateTime(new Date());
            userRoleMapper.addUserRole(userRoleEntity);
        }
        return Result.SUCCESS();
    }

    @Override
    @Transactional
    public Result updateUser(UserEntity user) {
        //查看修改是否重名
        UserEntity userEntity = userMapper.queryByName(user.getUsername());
        if (userEntity != null) {

            return Result.ERROR(ResultEnum.ADD_ERROR);
        }
        user.setUpdateTime(new Date());
        //开始修改
        userMapper.updateUserAll(user);
        //删除中间表
        userRoleMapper.deleteUserRole(user.getId());
        for (int i = 0; i < user.getRoleEntitylist().size(); i++) {
            UserRoleEntity userRoleEntity = new UserRoleEntity();
            RoleEntity roleEntity = user.getRoleEntitylist().get(i);
            userRoleEntity.setUserId(user.getId());
            userRoleEntity.setRoleId(roleEntity.getId());
            userRoleEntity.setCreateUser("毛毛");
            userRoleEntity.setUpdateUser("病病");
            userRoleEntity.setUpdateTime(new Date());
            userRoleMapper.addUserRole(userRoleEntity);
        }
        return Result.SUCCESS();
    }
}
